require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/tf_beverage_cabinet.zip"),
}

local prefabs =
{
    "collapse_small",
}

local invPrefabList = require("tf_minisign_list") --mod中有物品栏图片的prefabs的表
local invBuildMaps = { "tf_minisign1", "tf_minisign2", "tf_minisign3" }

local function SetShowSlot(inst, slot)
    local item = inst.components.container.slots[slot]
    if item == nil then
        inst.AnimState:ClearOverrideSymbol("slot" .. tostring(slot))
        inst.AnimState:ClearOverrideSymbol("slotbg" .. tostring(slot))
    else
        local atlas, bgimage, bgatlas
        local image = FunctionOrValue(item.drawimageoverride, item, inst) or (#(item.components.inventoryitem.imagename or "") > 0 and item.components.inventoryitem.imagename) or item.prefab or nil
        if image ~= nil then
            atlas = FunctionOrValue(item.drawatlasoverride, item, inst) or (#(item.components.inventoryitem.atlasname or "") > 0 and item.components.inventoryitem.atlasname) or nil
            if item.inv_image_bg ~= nil and item.inv_image_bg.image ~= nil and item.inv_image_bg.image:len() > 4 and item.inv_image_bg.image:sub(-4):lower() == ".tex" then
                bgimage = item.inv_image_bg.image:sub(1, -5)
                bgatlas = item.inv_image_bg.atlas ~= GetInventoryItemAtlas(item.inv_image_bg.image) and item.inv_image_bg.atlas or nil
            end

            if invPrefabList[image] ~= nil then
                inst.AnimState:OverrideSymbol("slot" .. tostring(slot), invBuildMaps[invPrefabList[image]] or invBuildMaps[1], image)
            else
                if atlas ~= nil then
                    atlas = resolvefilepath_soft(atlas)
                end
                inst.AnimState:OverrideSymbol("slot" .. tostring(slot), atlas or GetInventoryItemAtlas(image .. ".tex"), image .. ".tex")
            end
            if bgimage ~= nil then
                if invPrefabList[bgimage] ~= nil then
                    inst.AnimState:OverrideSymbol("slotbg" .. tostring(slot), invBuildMaps[invPrefabList[bgimage]] or invBuildMaps[1], bgimage)
                else
                    if bgatlas ~= nil then
                        bgatlas = resolvefilepath_soft(bgatlas)
                    end
                    inst.AnimState:OverrideSymbol("slotbg" .. tostring(slot), bgatlas or GetInventoryItemAtlas(bgimage .. ".tex"), bgimage .. ".tex")
                end
            else
                inst.AnimState:ClearOverrideSymbol("slotbg" .. tostring(slot))
            end
        else
            inst.AnimState:ClearOverrideSymbol("slot" .. tostring(slot))
            inst.AnimState:ClearOverrideSymbol("slotbg" .. tostring(slot))
        end
    end
end

local function ItemGet_chest(inst, data)
    if data and data.slot and data.slot <= 9 then
        SetShowSlot(inst, data.slot)
    end
end
local function ItemLose_chest(inst, data)
    if data and data.slot and data.slot <= 9 then
        SetShowSlot(inst, data.slot)
    end
end

local function onopen(inst)
    inst.AnimState:PlayAnimation("open")
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_open")
end

local function onclose(inst)
    inst.AnimState:PlayAnimation("close")
    inst.AnimState:PushAnimation("closed", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_close")
    for i = 1, 9, 1 do
        SetShowSlot(inst, i)
    end
end

local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    inst.components.container:DropEverything()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("metal")
    inst:Remove()
end

local function onhit(inst, worker)
    inst.AnimState:PlayAnimation("hit")
    inst.components.container:DropEverything()
    inst.AnimState:PushAnimation("closed", false)
    inst.components.container:Close()
end

local function onbuilt(inst)
    inst.AnimState:PlayAnimation("place")
    inst.AnimState:PushAnimation("closed", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_craft")
end

local function SetPerishRate(inst, item)
    if item == nil then
        return TUNING.TICKF
    end
    if item:HasTag("frozen") and TUNING.TICKF > 0 then
        return 0
    end
    return TUNING.TICKF
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(0.75) --recipe min_spacing/2

    inst.MiniMapEntity:SetIcon("tf_beverage_cabinet.tex")

    inst:AddTag("fridge")
    inst:AddTag("structure")

    inst.AnimState:SetBank("tf_beverage_cabinet")
    inst.AnimState:SetBuild("tf_beverage_cabinet")
    inst.AnimState:PlayAnimation("closed")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.shownum_l = 9 --后面的_l是棱镜的意思,不去掉是为了方便兼容统一,少写一个函数

    inst:AddComponent("inspectable")
    inst:AddComponent("container")
    inst.components.container:WidgetSetup("tf_beverage_cabinet")
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose
    inst.components.container.skipclosesnd = true
    inst.components.container.skipopensnd = true
    inst.components.container:EnableInfiniteStackSize(true) --启用无限堆叠

    inst:AddComponent("preserver")
    inst.components.preserver:SetPerishRateMultiplier(SetPerishRate)


    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(2)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)

    inst:ListenForEvent("onbuilt", onbuilt)
    inst:ListenForEvent("itemget", ItemGet_chest)
    inst:ListenForEvent("itemlose", ItemLose_chest)

    AddHauntableDropItemOrWork(inst)

    return inst
end

return Prefab("tf_beverage_cabinet", fn, assets, prefabs),
    MakePlacer("tf_beverage_cabinet_placer", "tf_beverage_cabinet", "tf_beverage_cabinet", "closed")
